﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net.Appender;
using Telerik.Web.UI;
using Aspose.Excel;

public partial class RainbowReport : BasePage
{
    public override string Icon
    {
        get
        {
            return "images/report_icon.png";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FilldropLstSelection();
        }
    }

    /// <summary>
    /// Fill dropLstSelection
    /// </summary>
    private void FilldropLstSelection()
    {
        DataLayer dal = new DataLayer();
        string message;

        dropLstSection.Items.Clear();

        dropLstSection.DataSource = dal.GetSections(PAConfiguration.ConnectionString, out message);
        dropLstSection.DataValueField = "section_id";
        dropLstSection.DataTextField = "section_title";
        dropLstSection.DataBind();

        dropLstSection.Items.Insert(0, new ListItem("Select a section", "0"));
    }

    private void LoadData()
    {
            DataLayer dal = new DataLayer();
            string message;

            DataTable dt = null;

            string name = tbName.Text;

            decimal? sectionId = null;
            if (dropLstSection.SelectedIndex > 1)
            {
                sectionId = Decimal.Parse(dropLstSection.SelectedItem.Value);
            }

            dt = dal.GetRainbowReportData(PAConfiguration.ConnectionString, Teacher.TEACHER_ID, name, sectionId, out message);

            if (dt == null)
            {
                rgRainbowReport.DataSource = null;
                return;
            }

            if (!string.IsNullOrEmpty(Sorting))
            {
                dt.DefaultView.Sort = Sorting;
            }

            rgRainbowReport.DataSource = dt;
    }

    protected void rgRainbowReport_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            DataTable dt = e.Item.OwnerTableView.DataSource as DataTable;
            if (dt != null)
            {
                //decimal val = (decimal)dt.Rows[e.Item.DataSetIndex]["GRADE_TO_DATE"];
                decimal val = Convert.ToDecimal(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["GRADE_TO_DATE"]);

                e.Item.Cells[6].Font.Bold = true;
                if (val <= (decimal)59.99)
                {
                    e.Item.Cells[6].BackColor = Color.FromName("Red");
                    e.Item.Cells[6].ForeColor = Color.White;
                }
                else if (val > (decimal)59.99 && val <= (decimal)69.99)
                {
                    e.Item.Cells[6].BackColor = Color.FromName("Yellow");
                }
                else
                {
                    e.Item.Cells[6].BackColor = Color.FromName("Green");
                    e.Item.Cells[6].ForeColor = Color.White;
                }
            }
        }
    }

    protected void lbExport_Click(object sender, ImageClickEventArgs e)
    {
      DataLayer dal = new DataLayer();
      string message = string.Empty;

      DataTable dt = null;

      string name = tbName.Text;
      decimal? sectionId = null;
      if (dropLstSection.SelectedIndex > 1)
      {
        sectionId = Decimal.Parse(dropLstSection.SelectedItem.Value);
      }
      dt = dal.GetRainbowReportData(PAConfiguration.ConnectionString, Teacher.TEACHER_ID, name, sectionId, out message);

      int startRow = 2;
      string[] listFields = new string[] { "STUDENT_NAME", "STUDENT_ID", "course_name", "OVERDUE_ASSIGNMENTS", 
        "GRADE_TO_DATE", "LAST_ASSIGNMENT_DATE", "LAST_ASSIGNMENT_DATE", 
        "EMAIL", "phone", "parent_name", "parent_email",
        "parent_phone"};
      Excel ex = Utils.GridToExcel(dt, listFields, "Rainbow Report", false, ref startRow);
      Worksheet ws = ex.Worksheets[0];
      
      for (int i = 0; i < dt.Rows.Count; ++i)
      {
        for (int j = 0; j < listFields.Length; ++j)
        {
          ws.Cells[i + 2, j].Style.Borders[BorderType.LeftBorder].Color = Color.Black;
          ws.Cells[i + 2, j].Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;

          ws.Cells[i + 2, j].Style.Borders[BorderType.RightBorder].Color = Color.Black;
          ws.Cells[i + 2, j].Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;

          ws.Cells[i + 2, j].Style.Borders[BorderType.TopBorder].Color = Color.Black;
          ws.Cells[i + 2, j].Style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;

          ws.Cells[i + 2, j].Style.Borders[BorderType.BottomBorder].Color = Color.Black;
          ws.Cells[i + 2, j].Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
        }
        
        ws.Cells[i + 2, 4].Style.Pattern = BackgroundType.Solid;

        decimal gtd = Convert.ToDecimal(dt.Rows[i]["GRADE_TO_DATE"]);

        ws.Cells[i + 2, 4].Style.HorizontalAlignment = TextAlignmentType.Right;
        ws.Cells[i + 2, 4].Style.Font.IsBold = true;
        if (gtd <= (decimal)59.99)
        {
          ws.Cells[i + 2, 4].Style.Font.Color = Color.White;
          ws.Cells[i + 2, 4].Style.ForegroundColor = Color.Red;
        }
        else if (gtd > (decimal)59.99 && gtd <= (decimal)69.99)
        {
          ws.Cells[i + 2, 4].Style.ForegroundColor = Color.Yellow;
        }
        else
        {
          ws.Cells[i + 2, 4].Style.Font.Color = Color.White;
          ws.Cells[i + 2, 4].Style.ForegroundColor = Color.Green;
        }

        ws.Cells[i + 2, 4].PutValue(ws.Cells[i + 2, 4].StringValue + "%");
      }

      ex.Save("Rainbow Report Download.xls", FileFormatType.Excel2003, SaveType.OpenInExcel, Page.Response);
      Page.Response.End();
    }

    protected void rgRainbowReport_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        LoadData();
    }
    
    protected void btnGo_Click(object sender, EventArgs args)
    {
        LoadData();
        rgRainbowReport.Rebind();
    }
}